home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- * GEMTERM V1.2
- * 1992 by Martin F. Gergeleit
- * placed in the public domain
- *
- * GEMTERM COMES WITH ABSOLUTELY NO WARRANTY, NOR WILL I BE LIABLE FOR ANY
- * DAMAGES INCURRED FROM THE USE OF IT. USE ENTIRELY AT YOUR OWN RISK!!!
- *********************************************************************/
-
- #include <gemfast.h>
- #include "txtwin.h"
- #include <basepage.h>
-
- #ifdef DEBUG
- #include <stdio.h>
- #endif
-
- extern int handle;
- extern GRECT screen;
-
- extern char copy_buffer[];
-
- char *get_hist_line();
-
- static char buffer[MAX_COLS];
- static short sx, sy, sc, sinv;
- static char *bp = 0;
-
- static char outstr[] = "x";
-
- static char hist_buffer[MAX_COLS];
-
- set_clip(x,y,w,h)
- int x,y,w,h;
- {
- int clip[4];
-
- #ifdef DEBUG
- fprintf(stderr, "set_clip (%d, %d, %d, %d)\n",x,y,w,h);
- #endif
-
- clip[0]=x;
- clip[1]=y;
- clip[2]=x+w-1;
- clip[3]=y+h-1;
- vs_clip(handle,1,clip);
- }
-
- txt2screen(win, tx, ty, sx, sy)
- txt_win *win;
- int tx, ty;
- int *sx, *sy;
- {
- *sx = win->xwork + (tx - win->x_start) * win->c_width;
- *sy = win->ywork + win->hwork - (win->y_start - ty + 1) * win->c_height;
-
- #ifdef DEBUG
- fprintf(stderr, "txt2screen (%d, %d) = (%d, %d)\n", tx, ty, *sx, *sy);
- #endif
- }
-
- print_str_at(win, x, y, str, inv)
- txt_win *win;
- int x, y;
- char *str;
- int inv;
-
- {
- GRECT t1,t2;
- int temp[4];
-
- #ifdef DEBUG
- fprintf(stderr, "print_str_at starts\n");
- #endif
-
- if (win->status != OPEN) {
- #ifdef DEBUG
- fprintf(stderr, "print_str_at fails (window not open)\n");
- #endif
- return;
- }
-
- txt2screen(win, x, y, &t2.g_x, &t2.g_y);
- t2.g_w= strlen(str) * win->c_width;
- t2.g_h= win->c_height;
-
- wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- while (t1.g_w && t1.g_h) {
- if (rc_intersect(&t2,&t1) && rc_intersect(&screen,&t1)) {
- set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
-
- if (inv) {
- temp[0] = t2.g_x;
- temp[1] = t2.g_y;
- temp[2]=temp[0] + t2.g_w - 1;
- temp[3]=temp[1] + win->c_height - 1;
- v_bar(handle,temp);
-
- vswr_mode(handle, 4);
- v_gtext(handle, t2.g_x, t2.g_y, str);
- vswr_mode(handle, 1);
- } else
- v_gtext(handle, t2.g_x, t2.g_y, str);
-
- }
- wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- }
-
- #ifdef DEBUG
- fprintf(stderr, "print_str_at ends\n");
- #endif
- }
-
- buff_print_at(win, x, y, c, inv)
- txt_win *win;
- int x, y;
- char c;
- int inv;
-
- {
- /* this is to much logging in most of the cases
- and what should happen in this code ? ;-)
- #ifdef DEBUG
- fprintf(stderr, "buff_print_at starts\n");
- #endif
- */
- if ((bp != 0) && ((sy != y) || (sx+sc != x) || (sinv != inv)))
- flush_buff(win);
-
- if (bp == 0) {
- sx = x;
- sy = y;
- sc = 1;
- sinv = inv;
- bp = buffer;
- *bp++ = c;
- *bp = '\0';
- /*
- #ifdef DEBUG
- fprintf(stderr, "buff_print_at ends\n");
- #endif
- */
- return;
- }
-
- *bp++ = c;
- *bp = '\0';
- sc++;
-
- /*
- #ifdef DEBUG
- fprintf(stderr, "buff_print_at ends\n");
- #endif
- */
- }
-
- flush_buff (win)
- txt_win *win;
-
- {
- #ifdef DEBUG
- fprintf(stderr, "flush_buff starts\n");
- #endif
-
- if (bp != 0) {
- print_str_at(win, sx, sy, buffer, sinv);
- bp = 0;
- }
-
- #ifdef DEBUG
- fprintf(stderr, "flush_buff ends\n");
- #endif
- }
-
- clear_area(win, x1, y1, x2, y2)
- txt_win *win;
- int x1, y1, x2, y2;
-
- {
- GRECT t1,t2;
- int temp[4];
-
- #ifdef DEBUG
- fprintf(stderr, "clear_area starts\n");
- #endif
-
- if (win->status != OPEN) {
- #ifdef DEBUG
- fprintf(stderr, "clear_area fails (window not open)\n");
- #endif
- return;
- }
-
- flush_buff (win);
-
- txt2screen(win, x1, y1, &temp[0], &temp[1]);
- txt2screen(win, x2, y2, &temp[2], &temp[3]);
- temp[2] += win->c_width;
- temp[3] += win->c_height;
-
- t2.g_x = temp[0];
- t2.g_y = temp[1];
- t2.g_w = temp[2] - temp[0];
- t2.g_h = temp[3] - temp[1];
-
- wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- while (t1.g_w && t1.g_h) {
- if (rc_intersect(&t2,&t1) && rc_intersect(&screen,&t1)) {
- set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
- v_bar(handle,temp);
- }
- wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- }
-
- #ifdef DEBUG
- fprintf(stderr, "clear_area ends\n");
- #endif
- }
-
- redraw_screen(win, rect)
- txt_win *win;
- GRECT *rect;
- {
- GRECT t1;
- int temp[4];
- int from, to, i;
- char *str;
- int x, y;
- char c;
-
- #ifdef DEBUG
- fprintf(stderr, "redraw_screen starts\n");
- #endif
-
- if (win->status != OPEN) {
- #ifdef DEBUG
- fprintf(stderr, "redraw_screen fails (window not open)\n");
- #endif
- return;
- }
-
- from = win->lines_written - (win->hwork/win->c_height - win->y_start - 1);
- to = (win->y_start) < 0 ? win->lines_written + win->y_start + 1 :
- win->lines_written;
-
- wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- while (t1.g_w && t1.g_h) {
- if (rc_intersect(rect,&t1) && rc_intersect(&screen,&t1)) {
- set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
-
- if (from < to)
- str = get_hist_line(win, from);
-
- temp[0] = win->xwork;
- temp[1] = win->ywork;
- temp[2] = win->xwork + win->wwork - 1;
- temp[3] = win->ywork + win->hwork - 1;
- v_bar(handle,temp);
-
- for (i = from; i < to; i++) {
- str = get_hist_line(win, i);
- if (strlen(str) > win->x_start)
- v_gtext(handle, win->xwork, win->ywork+ (i-from) * win->c_height,
- &str[win->x_start]);
- }
-
- if (win->y_start >= 0) {
- temp[1] = win->ywork + (i-from) * win->c_height -1;
- temp[2] = win->xwork + win->wwork - 1;
- temp[3] = temp[1];
- v_pline(handle, 2, temp);
- }
- }
- wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- }
-
- for (y = 0; y < win->lines; y++)
- for (x = 0; (c = win->wtext[y * (win->cols+1) + x]) != '\0'; x++)
- buff_print_at(win, x, y, c, win->attr[y * win->cols + x]);
-
- flush_buff(win);
- cursor(win,TRUE);
-
- #ifdef DEBUG
- fprintf(stderr, "redraw_screen ends\n");
- #endif
- }
-
- scroll_screen(win, lines)
- txt_win *win;
- short lines;
-
- {
- int array[8];
- MFDB screenMFDB;
- GRECT t1;
- int tx, ty;
- short i;
- char c;
- char line_select[win->lines];
-
- #ifdef DEBUG
- fprintf(stderr, "scroll_screen starts\n");
- #endif
-
- if (win->status != OPEN) {
- #ifdef DEBUG
- fprintf(stderr, "scroll_screen fails (window not open)\n");
- #endif
- return;
- }
-
- flush_buff(win);
-
- for (i=0; i < win->lines; i++)
- line_select[i] = FALSE;
-
- array[0] = win->xwork;
- array[1] = win->ywork + lines * win->c_height;
- array[2] = win->xwork + win->wwork - 1;
- array[3] = win->ywork + win->hwork - 1;
- if (array[3] > screen.g_y + screen.g_h - 1)
- array[3] = screen.g_y + screen.g_h - 1;
- array[4] = win->xwork;
- array[5] = win->ywork;
- array[6] = array[2];
- array[7] = array[3] - lines * win->c_height;
- screenMFDB.fd_addr = 0;
-
- wind_get(win->handle,WF_FIRSTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- while (t1.g_w && t1.g_h) {
- if (rc_intersect(&screen,&t1)) {
- screen2txt(win, t1.g_x + t1.g_w - 1, t1.g_y + t1.g_h - 1, &tx, &ty);
- for (i = 0; (i <= lines) && (ty - i >= 0); i++)
- line_select[ty - i] = TRUE;
- if ((t1.g_h -= lines * win->c_height) > 0) {
- set_clip(t1.g_x,t1.g_y,t1.g_w,t1.g_h);
- vro_cpyfm(handle, 3, array, &screenMFDB, &screenMFDB);
- }
- }
- wind_get(win->handle,WF_NEXTXYWH,&t1.g_x,&t1.g_y,&t1.g_w,&t1.g_h);
- }
-
- for (ty = 0; ty < win->lines; ty++)
- if (line_select[ty]) {
- for (tx = 0; (c = win->wtext[ty * (win->cols+1) + tx]) != '\0'; tx++)
- buff_print_at(win, tx, ty, c, win->attr[ty * win->cols + tx]);
-
- flush_buff(win);
- clear_area(win, tx, ty, win->cols - 1, ty);
- }
-
- #ifdef DEBUG
- fprintf(stderr, "scroll_screen ends\n");
- #endif
- }
-
- cursor(win, on)
- txt_win *win;
- int on;
- {
- int flag;
-
- #ifdef DEBUG
- fprintf(stderr, "cursor starts\n");
- #endif
-
- *outstr = win->wtext[win->y_cursor * (win->cols+1) + win->x_cursor];
- if (*outstr >= ' ') {
- flag = win->attr[win->y_cursor * win->cols + win->x_cursor];
- print_str_at(win, win->x_cursor, win->y_cursor, outstr, on ? ~flag : flag);
- }
- else
- print_str_at(win, win->x_cursor, win->y_cursor, " ", on);
-
- #ifdef DEBUG
- fprintf(stderr, "cursor ends\n");
- #endif
- }
-
- screen2txt(win, sx, sy, tx, ty)
- txt_win *win;
- int sx, sy;
- int *tx, *ty;
- {
- *tx = (sx - win->xwork) / win->c_width + win->x_start;
- *ty = win->y_start - (win->ywork + win->hwork - sy) / win->c_height;
-
- #ifdef DEBUG
- fprintf(stderr, "screen2txt (%d, %d) = (%d, %d)\n", sx, sy, *tx, *ty);
- #endif
- }
-
- put_in_hist(win, str)
- txt_win *win;
- char *str;
- {
- short del_line = FALSE;
-
- #ifdef DEBUG
- fprintf(stderr, "put_in_hist starts\n");
- #endif
-
- if(win->history == 0) {
- #ifdef DEBUG
- fprintf(stderr, "put_in_hist ends\n");
- #endif
- return;
- }
- win->last_out = win->hist_size + 1;
- win->lines_written++;
- do {
- if (win->hist_in == win->hist_out)
- del_line = TRUE;
- if (del_line && (win->history[win->hist_in] == '\0'))
- win->lines_written--;
- win->history[win->hist_in] = *str;
- win->hist_in = (win->hist_in + 1) % win->hist_size;
- } while (*str++ != '\0');
-
- if (!del_line) {
- #ifdef DEBUG
- fprintf(stderr, "put_in_hist ends\n");
- #endif
- return;
- }
-
- win->hist_out = win->hist_in;
- while (win->history[win->hist_out] != '\0')
- win->hist_out = (win->hist_out + 1) % win->hist_size;
-
- #ifdef DEBUG
- fprintf(stderr, "put_in_hist ends\n");
- #endif
- }
-
- char *get_hist_line(win, number)
- txt_win *win;
- int number;
- {
- register int i;
- int count;
- char *p;
-
- #ifdef DEBUG
- fprintf(stderr, "get_hist_line starts\n");
- #endif
-
- if(win->history == 0) {
- #ifdef DEBUG
- fprintf(stderr, "get_hist_line ends\n");
- #endif
- return "";
- }
- if (number >= win->last_out) {
- i = win->last_index;
- count = number - win->last_out;
- }
- else {
- i = (win->hist_out + 1) % win->hist_size;
- count = number;
- }
-
- while (count > 0) {
- if (win->history[i] == '\0')
- count --;
- i = (i+1) % win->hist_size;
- }
-
- win->last_out = number;
- win->last_index = i;
-
- p = hist_buffer;
- do {
- *p = win->history[i];
- i = (i+1) % win->hist_size;
- } while (*p++ != '\0');
-
- #ifdef DEBUG
- fprintf(stderr, "get_hist_line ends\n");
- #endif
- return hist_buffer;
- }
-
- update_slider(win)
- txt_win *win;
- {
- int current_lines, current_cols;
- int tmp;
-
- #ifdef DEBUG
- fprintf(stderr, "update_slider starts\n");
- #endif
-
- if (win->status != OPEN) {
- #ifdef DEBUG
- fprintf(stderr, "update_slider fails (window not open)\n");
- #endif
- return;
- }
-
- current_cols = win->wwork/win->c_width;
- current_lines = win->hwork/win->c_height;
-
- tmp = current_cols*1000/win->cols;
- if (win->hs_s != tmp) {
- win->hs_s = tmp;
- wind_set(win->handle, WF_HSLSIZE,tmp,0,0,0);
- }
-
- tmp = (current_cols == win->cols) ? 1 : (win->x_start*999)/(win->cols-current_cols)+1;
- if (win->hs_p != tmp) {
- win->hs_p = tmp;
- wind_set(win->handle, WF_HSLIDE,tmp,0,0,0);
- }
-
- tmp = (current_lines)*999/(win->lines+win->lines_written) + 1;
- if (win->vs_s != tmp) {
- win->vs_s = tmp;
- wind_set(win->handle, WF_VSLSIZE,tmp,0,0,0);
- }
-
- tmp = (win->lines_written + win->lines - current_lines == 0) ? 1000:
- (win->lines_written - current_lines + win->y_start + 1) * 999
- / (win->lines_written + win->lines - current_lines)+1;
- if (win->vs_p != tmp) {
- win->vs_p = tmp;
- wind_set(win->handle, WF_VSLIDE,tmp,0,0,0);
- }
- #ifdef DEBUG
- fprintf(stderr, "update_slider ends\n");
- #endif
- }
-
- char *get_line(win, n)
- txt_win *win;
- int n;
- {
- int i;
-
- #ifdef DEBUG
- fprintf(stderr, "get_line starts\n");
- #endif
-
- for (i = 0; i < MAX_COLS; i++)
- buffer[i] = '\0';
-
- if (n < 0)
- strcpy(buffer, get_hist_line(win, win->lines_written + n));
- else
- strcpy(buffer, &win->wtext[n * (win->cols+1)]);
-
- #ifdef DEBUG
- fprintf(stderr, "get_line ends\n");
- #endif
- return buffer;
- }
-
- copy_to_scrapbuf(win, tx1, ty1, tx2, ty2)
- txt_win *win;
- int tx1, ty1, tx2, ty2;
- {
- char *str;
- int i;
-
- #ifdef DEBUG
- fprintf(stderr, "copy_to_scrapbuf starts\n");
- #endif
-
- if (ty1 > ty2) {
- i = ty1;
- ty1 = ty2;
- ty2 = i;
- }
- if (tx1 > tx2) {
- i = tx1;
- tx1 = tx2;
- tx2 = i;
- }
-
- str = get_line(win, ty1);
- strcpy(copy_buffer, &str[tx1]);
-
- if (ty1 == ty2) {
- copy_buffer[(tx2-tx1) +1] = '\0';
- if (strlen(str) <= tx2)
- strcat(copy_buffer, "\n");
- #ifdef DEBUG
- fprintf(stderr, "copy_to_scrapbuf ends\n");
- #endif
- return;
- }
-
- ty1++;
- while (ty1 < ty2) {
- strcat(copy_buffer, "\n");
- str = get_line(win, ty1);
- if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 2))
- strcat(copy_buffer, str);
- else {
- #ifdef DEBUG
- fprintf(stderr, "copy_to_scrapbuf ends\n");
- #endif
- return;
- }
- ty1++;
- }
-
- strcat(copy_buffer, "\n");
- str = get_line(win, ty2);
- if (strlen(str) <= tx2) {
- if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 2)) {
- strcat(copy_buffer, str);
- strcat(copy_buffer, "\n");
- };
- }
- else {
- str[tx2+1] = '\0';
- if ((strlen(str) + strlen(copy_buffer)) < (MAXCOPYLEN - 1))
- strcat(copy_buffer, str);
- }
- #ifdef DEBUG
- fprintf(stderr, "copy_to_scrapbuf ends\n");
- #endif
- }
-
- char *my_getenv(varname)
- char *varname;
- {
- char *var;
- int len;
-
- #ifdef DEBUG
- fprintf(stderr, "my_getenv starts (%s)\n", varname);
- #endif
-
- len = strlen(varname);
- if (_base->p_env == 0) {
- #ifdef DEBUG
- fprintf(stderr, "my_getenv returns 0\n");
- #endif
- return 0;
- }
-
- for (var = _base->p_env; *var != '\0'; var++) {
- if (!strncmp(var, varname, len) && var[len] == '=') {
- #ifdef DEBUG
- fprintf(stderr, "my_getenv returns %s\n", var+len+1);
- #endif
- return var+len+1;
- }
- while (*var != '\0')
- var++;
- }
- #ifdef DEBUG
- fprintf(stderr, "my_getenv returns 0\n");
- #endif
- return 0;
- }
-
-